package com.dream.master.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dream.dao.master.ProductCategoryDAO;
import com.dream.master.service.ProductCategoryService;
import com.dream.model.app.AppAbstractUser;
import com.dream.model.master.ProductCategory;
import com.dream.vo.base.TreeNodeVO;
import com.google.common.collect.Lists;

@Service("productCategoryService")
public class ProductCategoryServiceImpl implements ProductCategoryService {
	
	@Autowired
	private ProductCategoryDAO productCategoryDAO;
	
	@Override
	@Transactional
	public ProductCategory getCategory(final Long checkedId) {
		
		ProductCategory root = productCategoryDAO.getRoot();
		if(checkedId != null && checkedId.compareTo(0L)<0){
			root.setChecked(true);
		}
		loopChildren(root,checkedId);
		return root;
	}
	@Transactional
	private void loopChildren(final ProductCategory root,final Long checkedId){
		
		if(root != null && root.getId().equals(checkedId)){
			root.setChecked(true);
		}
		List<ProductCategory> children = productCategoryDAO.getChildren(root.getId());
		root.setChildren(children);
		if(CollectionUtils.isNotEmpty(children)){
			for(ProductCategory item : children){
				loopChildren(item,checkedId);
			}
		}
	}

	@Override
	@Transactional
	public ProductCategory getChildrens(Long parentId) {
		
		if(parentId == null || parentId.compareTo(0L)<0){
			ProductCategory root = productCategoryDAO.getRoot();
			parentId = root.getId();
			List<ProductCategory> children = productCategoryDAO.getChildren(parentId);
			root.setChildren(children);
			return root;
		}else{
			ProductCategory parent = productCategoryDAO.selectById(parentId);
			List<ProductCategory> children = productCategoryDAO.getChildren(parentId);
			parent.setChildren(children);
			return parent;
		}
		
	
	}

	@Override
	@Transactional
	public void updateCategory(TreeNodeVO vo, AppAbstractUser user) {
		
		if(vo.getId() == null ){
			
			ProductCategory  newItem = new 	ProductCategory();
			newItem.setId(vo.getId());
			newItem.setName(vo.getName());
			newItem.setParentId(vo.getParentId());
			ProductCategory parent = productCategoryDAO.selectById(vo.getParentId());
			newItem.setLevel(parent.getLevel()+1);
			
			newItem.setCreateTime(new Date());
			newItem.setCreateUser(user.getId());
			productCategoryDAO.save(newItem);
			
		}else{
			ProductCategory me = productCategoryDAO.selectById(vo.getId());
			me.setName(vo.getName());
			me.setUpdateTime(new Date());
			me.setUpdateUser(user.getId());
			productCategoryDAO.saveOrUpdate(me);
		}
	}

	@Override
	@Transactional
	public void deleteCategory(Long id, AppAbstractUser user) {
		
		ProductCategory me = productCategoryDAO.selectById(id);
		me.setDeleted(true);
		productCategoryDAO.saveOrUpdate(me);
	}

	@Override
	@Transactional
	public List<Long> getCats(String first, String second) {
		
		List<Long> li = Lists.newArrayList();
		if(StringUtils.isNotBlank(second)){
			li.add(Long.parseLong(second));
		}else if(StringUtils.isNotBlank(first)){
			List<ProductCategory> children = productCategoryDAO.getChildren(Long.parseLong(first));
			if(CollectionUtils.isNotEmpty(children)){
				for(ProductCategory item : children){
					li.add(item.getId());
				}
			}
		}
		
		return li;
	}

	@Override
	@Transactional
	public List<ProductCategory> getParent(Long id) {
		
		List<ProductCategory> li = Lists.newArrayList();
		while(id.compareTo(0L)>0){
			ProductCategory me = productCategoryDAO.selectById(id);
			li.add(me);
			id = me.getParentId();
		}
		return li;
	}
	
	
}
